import Ball from "./GameBall"
import Circle from "./GameCirclet"
import Line from './GameLine'
import Prop from './GameProp'
import Grade from './GameGrade'
import Common from '../Common/common'
import Sprite = Laya.Sprite;
import Tween = Laya.Tween;
import Image = Laya.Image
import { Data } from "../Data/data"
// 游戏管理类
export default class Mgr extends Laya.Scene {
    [x: string]: any;

    public ball;  //小球
    public isSendBall: boolean = false;
    public static circleList: any[] = [];//环数组
    public circularList: any[] = [];//覆盖小球数组
    public angle: any
    public C = new Circle()
    public b = new Ball();
    public multiple
    public degrees
    public lineList = []
    public spot = []
    public line = {
        x0: null,
        y0: null,
        x: null,
        y: null
    }
    public propList = []
    public Arr = [170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, 310, 556, 330, 340, 350, 360, 370, 380, 390, 696, 410, 420, 430, 440, 450, 460, 470]
    public lv = 1
    public num = 1
    public countDownNum = 1
    public isKey: boolean
    public isAdd: boolean = false
    public isResurrection = true
    public gapAngle = 70
    public tween //缓动对象
    public meteorTween//流星缓动对象
    public line1
    public line2
    public isTap: boolean = true
    public time // 匀速运动时间
    public level: number = 0 //等级
    public boom
    public starLv: number = 0//到达的星球
    public meteorList = [];

    constructor() {
        super()

        Laya.stage.alignV = "middle";
        //水平居中对齐，另一种写法：Laya.stage.alignH = Stage.ALIGN_CENTER;
        Laya.stage.alignH = "center";

        Laya.Event.TRIGGER_ENTER
    }

    /**
     * 监听打开场景
     */
    onOpened(): void {
        let s = Laya.timer.toString();
        console.log(s);
        let nDate=new Date();
        let pT=Date.parse(nDate.toUTCString());
        this.initData()
        let randomX: number = this.getRandom()
        Laya.stage.event("playMusic", ["sound/homeBg.mp3", true])
        let c = new Circle()
        let c1 = new Circle()
        let c2 = new Circle()
        Mgr.circleList.push(c.circle(540, 1100, 200));
        this.circularList.push(c.drawMidCircle(540, 1100, 100))
        Mgr.circleList.push(this.C.circle(randomX, 400, 200));//初始化环
        this.circularList.push(this.C.drawMidCircle(randomX, 400, 100))
        Mgr.circleList.push(c1.circle(540, -300, 200));
        this.circularList.push(c1.drawMidCircle(540, -300, 100))
        Mgr.circleList.push(c2.circle(540, -1000, 200));
        this.circularList.push(c2.drawMidCircle(540, -1000, 100))
        Laya.stage.frameLoop(1, this, this.getAngle)
        console.log("circleList1111:" + Mgr.circleList)
        this.ballMgr();
        // this.newlyAdded();
        this.rotateAni();
        Laya.stage.once("resurrectionTween", this, this.resurrectionTween);
        Laya.stage.once("isDouble", this, this.isDouble)
        Laya.stage.once('gameOver', this, this.gameOver)
        // Laya.stage.once('newlyAdded', this, this.newlyAdded)
        Laya.stage.event("RecordStart")

        //提示
        this.showAndHideTips();
        Common.timeArray.push(this);

    }


    /**
     * 获取人物占据度数
     */
    getRadian(): number {
        let radian
        if (Common.getCacheItem("character") == "1" || Common.getCacheItem("character") == null) {
            radian = Math.asin((this.ball.height / 2) / ((Mgr.circleList[0].width) / 2));
        } else {
            radian = Math.asin((this.ball.width / 2) / (Mgr.circleList[0].width / 2));
        }

        radian = radian / (Math.PI / 180)//求出的值是弧度,弧度转化为角度
        return radian
    }

    /**初始化场景数据 */
    initData() {
        Data.GameData.starNum = 1
        Data.GameData.meteoriteNum = 5
        this.isSendBall = false;
        // Mgr.circleList.length = 0
        // this.circularList.length = 0
        this.angle = null
        this.C = new Circle()
        this.b = new Ball();
        this.degrees = null
        // this.lineList.length = 0
        // this.spot.length = 0
        this.line = {
            x0: null,
            y0: null,
            x: null,
            y: null
        }
        // this.propList.length = 0
        this.isKey = false
        this.isAdd = true
        this.isResurrection = true
        this.gapAngle = 70
        // console.log("circleList:"+Mgr.circleList)
        // console.log("circularList:"+this.circularList)
        // console.log("lineList:"+this.lineList)
        // console.log("spot:"+this.spot)
    }

    /**
     * 
     * @param tips 开启关闭提示
     */
    showAndHideTips(tips: string = null) {
        if (tips != null) this.tips.text = tips;
        this.tips.visible = true;
        //3秒后关闭提示
        Laya.timer.once(3000, this, () => {
            this.tips.visible = false;
        });
    }

    /**
     * 
     * @param tips 开启关闭道具提示
     */
    showPropTips(tips: string = null) {
        if (tips != null) this.propTips.text = tips;
        this.propTips.visible = true;
        this.propTips.y = this.ball.y
        //1.5秒后关闭提示
        Tween.to(this.propTips, { y: this.ball.y - 80 }, 1500)
        Laya.timer.once(1500, this, () => {
            this.propTips.visible = false;
        });
    }

    /**
     * 随机生成道具
     */
    randomProp() {
        let prop = new Prop()
        let random = Math.random()
        let position = this.propPosition()
        let type = random <= 0.25 ? 0 : random <= 0.5 ? 1 : random <= 0.75 ? 2 : 3
        if (type == 0) {
            //提示
            this.showAndHideTips("钻石出现8秒后消失");
            this.propList.push(prop.generateDiamonds(position.x, position.y))
        } else if (type == 2) {
            if (this.starLv >= 7) {
                this.showAndHideTips("钥匙出现8秒后消失，获得后环速变慢");
                this.propList.push(prop.generateKey(position.x, position.y))
            }
        }
        if (this.propList.length != 0) {
            Laya.timer.once(8000, this, () => {
                Laya.stage.removeChild(this.propList[0])
                this.propList.splice(0, 1)
            });
        }
    }

    /**
     * 道具定位
     */
    propPosition(): any {
        let ringList
        let starList
        console.log("propList:",this.propList)
        console.log("circleList",Mgr.circleList)
        console.log("circularList",this.circularList)
        if (this.propList.length > 0) {
            ringList = this.getInsertPointBetweenCircleAndLine(Mgr.circleList[this.propList.length - 1].width / 2 - 20, 0)
            starList = this.getInsertPointBetweenCircleAndLine(this.circularList[this.propList.length - 1].width / 2 - 20, 0)
        } else {
            ringList = this.getInsertPointBetweenCircleAndLine(Mgr.circleList[0].width / 2 - 20, 0)
            starList = this.getInsertPointBetweenCircleAndLine(this.circularList[0].width / 2 - 20, 0)
        }
        let position = {}
        ringList.forEach((i, j) => {
            if (i.y > Mgr.circleList[0].y) {
                if (starList[j].y > Mgr.circleList[0].y) {
                    if (this.ball.x == Mgr.circleList[0].x) {
                        position = { x: this.ball.x, y: Mgr.circleList[0].y + (Mgr.circleList[0].width - this.circularList[0].width) }
                    } else {
                        position = { x: (i.x + starList[j].x) / 2, y: (i.y + starList[j].y) / 2 }
                    }
                }
            }
        })
        return position
    }

    /**
     * 道具使用
     */
    useProp() {
        // if(this.propList.length != 0){
        //     let px = this.propList[0].x
        //     let py = this.propList[0].y
        //     let isUse = this.equation(px,py)
        //     if(isUse){
        //         if(this.propList[0].width == 30){
        //             console.log("吃了钻石")
        //         }else{
        //             console.log("吃了钥匙")
        //         }
        //     }
        // }
        console.log(11111)
        if (this.propList.length != 0) {
            console.log(2222)
            if (this.ball.getBounds().intersection(this.propList[0].getBounds())) {
                if (this.propList[0].name == "key") {
                    this.rotateAni()
                    Laya.stage.removeChild(this.propList[0])
                    this.propList.splice(0, 1)
                    this.showPropTips("环旋转速度变慢，10秒后恢复正常！");
                } else {
                    let gemstoneNum = Number(this.gemstone.text)
                    this.gemstone.text = gemstoneNum += 5
                    Laya.stage.removeChild(this.propList[0])
                    this.propList.splice(0, 1)
                    this.showPropTips("+5钻石");
                }
            }
        }
    }

    /**
     * 吃了钥匙
     * 所有环旋转速度减少
     * 10秒
     */
    rotateAni() {
        //提示
        //this.showAndHideTips("环旋转速度变慢，10秒后恢复正常！");
        Laya.stage.event("clear")
        Laya.timer.loop(1000, this, this.countDown)
    }

    /**
     * 倒计时10秒
     * 环旋转减速
     */
    countDown() {

        if (this.num <= 10) {
            this.num++
        } else {
            Laya.stage.event("rotate")
            Laya.timer.clear(this, this.countDown)
        }
    }


    /**
     * 求一元一次方程
     * 判断是否在同一线
     * ax+by+c=0
     */
    // equation(x1:number , y1:number):boolean {
    //     let x = this.ball.x
    //     let y = this.ball.y
    //     let x0 = Mgr.circleList[0].x
    //     let y0 = Mgr.circleList[0].y
    //     let isLine:boolean
    //     let a=y0-y

    //     let b=x-x0

    //     let c=-a*x+b*y)

    //     if((-(a*x1+c))/b == y1){
    //         isLine = true
    //     }else{
    //         isLine = false
    //     }
    //     return isLine


    // }

    /**
     * 画线
     */
    drawLine(): void {
        let l = new Line()
        this.lineList.push(l.drawLine(this.line.x0, this.line.y0, this.line.x, this.line.y))
        this.spot.push(l.drawSpot(this.line.x0, this.line.y0))
    }


    /**
     * 监听点击飞机
     */
    onMouseDown(): void {
        Laya.stage.event("playSound", ["sound/tap.mp3"])
        if (this.isTap) {
            if (this.isAdd) {
                Laya.stage.clearTimer(this, this.decline)
            }
            this.earth.removeSelf();
            this.degrees = this.getDegrees()
            this.motion()
            this.isTap = false
        }
    }

    /**
     * 飞机运动
     */
    motion(): void {
        let x = this.ball.x
        let y = this.ball.y
        let x0 = Mgr.circleList[0].x
        let y0 = Mgr.circleList[0].y
        let r = Mgr.circleList[0].width / 2
        let overlapping: any[] = this.getInsertPointBetweenCircleAndLine()
        console.log("overlapping:", overlapping)
        this.line.x0 = x
        this.line.y0 = y
        this.line.x = x0
        this.line.y = y0
        if (x == x0) {
            this.line1 = y - y0
        } else if (x > x0) {
            this.line1 = Common.pythagorean(x0, y0, x, y)
        } else if (x < x0) {
            this.line1 = Common.pythagorean(x, y0, x0, y)
        }
        this.time = this.line1 / 1000
        console.log("line:", this.line1)
        console.log("time:", this.time)
        if (x == x0) {//飞机与环垂直居中
            this.ball.rotation = 0
            if (Common.getCacheItem("character") == "1" || Common.getCacheItem("character") == null) {
                this.ball.rotation -= 90
            }
            this.tween = Tween.to(this.ball, { y: y0 + r }, 1000 * this.time, Laya.Ease.linearInOut, Laya.Handler.create(this, this.ballOnLoop));
            // this.tween.update = new Laya.Handler(this, (res) => {
            //     // console.log(this.ball.y)
            //     // console.log(y0 + r)
            //     // console.log(this.ball.y < (y0 + r))
            //     // if (this.ball.y < (y0 + r)) {
            //     //     console.log("执行判断")
            //     //     this.ballOnLoop()
            //     // }
            //     if(this.ball.getBounds().intersection(Mgr.circleList[0].getBounds())){
            //         console.log("执行判断")
            //         this.ballOnLoop()
            //     }
            // })
        } else {

            overlapping.forEach(item => {
                if (item.y > y0) {
                    if (x < x0) {
                        if (Common.getCacheItem("character") == "1" || Common.getCacheItem("character") == null) {
                            this.ball.rotation = 0
                        } else {
                            this.ball.rotation = 90
                        }
                        this.ball.rotation += this.degrees
                    } else if (x > x0) {
                        if (Common.getCacheItem("character") == "1" || Common.getCacheItem("character") == null) {
                            this.ball.rotation = 180
                        } else {
                            this.ball.rotation = 270
                        }
                        this.ball.rotation += this.degrees
                    }
                    this.tween = Tween.to(this.ball, { x: item.x, y: item.y }, 1000 * this.time, Laya.Ease.linearInOut, Laya.Handler.create(this, this.ballOnLoop));
                    // this.tween.update = new Laya.Handler(this, (res) => {
                    //     // console.log("overlapping", overlapping)
                    //     // overlapping.forEach(item => {
                    //     // console.log("item", item)
                    //     // if (item.y > y0) {
                    //     // console.log(res, this.ball.y)
                    //     // console.log("item.y", item.y)
                    //     // if (this.ball.y < item.y) {
                    //     //     console.log("执行判断")
                    //     //     this.ballOnLoop()
                    //     // }
                    //     // }
                    //     if(this.ball.getBounds().intersection(Mgr.circleList[0].getBounds())){
                    //         console.log("执行判断")
                    //         this.ballOnLoop()
                    //     }
                    // })
                    // })
                }
            })
        }
    }

    /**
    * 清除环和画线
    * 吃道具
    */
    remove(): void {
        this.showPropTips("+100")
        this.starLv++
        if (this.starLv == 1) {
            this.showAndHideTips("已到达金星");
            this.level = 1
        } else if (this.starLv == 7) {
            this.showAndHideTips("已到达水星");
            this.level = 2
        } else if (this.starLv == 13) {
            this.showAndHideTips("已到达火星");
            this.level = 3
        } else if (this.starLv == 19) {
            this.showAndHideTips("已到达木星");
            this.level = 4
        } else if (this.starLv == 25) {
            this.showAndHideTips("已到达土星");
            this.level = 5
        } else if (this.starLv == 31) {
            this.showAndHideTips("已到达天王星");
            this.level = 6
        } else if (this.starLv == 37) {
            this.showAndHideTips("已到达海王星");
            this.level = 7
        } else if (this.starLv == 43) {
            this.showAndHideTips("已到达宇宙太空站");
            this.level = 8
        }
        let fractionNum = Number(this.fraction.text)
        Laya.timer.clear(this, this.useProp)
        Laya.stage.removeChild(Mgr.circleList[0])
        Laya.stage.removeChild(this.circularList[0])
        this.fraction.text = fractionNum += 100
        Mgr.circleList.splice(0, 1)
        this.circularList.splice(0, 1)
        this.drawLine()
        Laya.timer.frameLoop(1, this, this.decline);
        this.isAdd = true
        this.isTap = true
        Laya.timer.once(100, this, this.newlyAdded)
    }

    /**
    * 监听飞机碰撞
    */
    ballOnLoop(): void {
        let x = this.ball.x
        let y = this.ball.y
        let x0 = Mgr.circleList[0].x
        let y0 = Mgr.circleList[0].y
        let overlapping: any[] = this.getInsertPointBetweenCircleAndLine()
        let r = Mgr.circleList[0].width / 2
        let radian = this.getRadian()
        // let radian = 0
        if (x0 == x) {
            this.line2 = Mgr.circleList[0].width / 2
        }
        if (x > x0) {
            this.line2 = Common.pythagorean(x0, y0, x, y)
        } else if (x < x0) {
            this.line2 = Common.pythagorean(x, y0, x0, y)
        }
        //1秒走1000需要走多久
        let time = this.line2 / 1000
        if (x < x0) {
            if ((180 + this.gapAngle / 2) + (90 + this.degrees) - radian >= this.angle && this.angle >= (180 - this.gapAngle / 2) + (90 + this.degrees) + radian) {
                console.log("过关啦")
                Laya.stage.timer.frameLoop(1, this, this.useProp)
                Tween.to(this.ball, { x: x0, y: y0 }, time * 1000);
                Laya.timer.once(time * 1000, this, this.remove);
            } else {
                Laya.stage.event("playSound", ["sound/boom.mp3"])
                overlapping.forEach(item => {
                    if (item.y > y0) {
                        this.boom = new Image();
                        this.boom.skin = "images/boom.png"
                        this.boom.size(140, 90)
                        this.boom.pivot(70, 45).pos(item.x, item.y)
                        this.boom.rotation = 90
                        this.boom.rotation += this.degrees
                        Laya.stage.addChild(this.boom)
                        this.boom.zOrder = 5
                    }
                })
                console.log("挂掉了")
                Laya.timer.clear(this, this.meteorCollision)
                // Tween.clear(this.tween)
                if (this.isResurrection) {
                    console.log("22222", Mgr.circleList)
                    // Laya.timer.once(1000, this, this.resurrection);
                    this.resurrection()
                } else {
                    // Laya.timer.once(1000, this, this.gameOver);
                    this.isDouble()
                }
            }
        } else if (x > x0) {
            if ((180 + this.gapAngle / 2) - (90 - this.degrees) - radian >= this.angle && this.angle >= (180 - this.gapAngle / 2) - (90 - this.degrees) + radian) {
                console.log("过关啦")
                Laya.stage.timer.frameLoop(1, this, this.useProp)
                Tween.to(this.ball, { x: x0, y: y0 }, time * 1000);
                Laya.timer.once(time * 1000, this, this.remove);
            } else {
                console.log("挂掉了")
                // Tween.clear(this.tween)
                Laya.stage.event("playSound", ["sound/boom.mp3"])
                overlapping.forEach(item => {
                    if (item.y > y0) {
                        this.boom = new Image();
                        this.boom.skin = "images/boom.png"
                        this.boom.size(140, 90)
                        this.boom.pivot(70, 45).pos(item.x, item.y)
                        this.boom.rotation = 270
                        this.boom.rotation += this.degrees
                        Laya.stage.addChild(this.boom)
                        this.boom.zOrder = 5
                    }
                })
                Laya.timer.clear(this, this.meteorCollision)
                if (this.isResurrection) {
                    console.log("22222", Mgr.circleList)
                    // Laya.timer.once(1000, this, this.resurrection);
                    this.resurrection()
                } else {
                    // Laya.timer.once(1000, this, this.gameOver);
                    this.isDouble()
                }
            }
        } else if (x == x0) {
            if (180 + this.gapAngle / 2 - radian >= this.angle && this.angle >= 180 - this.gapAngle / 2 + radian) {
                console.log("过关啦")
                Laya.stage.timer.frameLoop(1, this, this.useProp)
                Tween.to(this.ball, { y: y0 }, time * 1000);
                Laya.timer.once(time * 1000, this, this.remove);
            } else {
                Laya.stage.event("playSound", ["sound/boom.mp3"])
                this.boom = new Image();
                this.boom.skin = "images/boom.png"
                this.boom.size(140, 90)
                this.boom.pivot(70, 45).pos(x0, y0 - r)
                Laya.stage.addChild(this.boom)
                this.boom.zOrder = 5
                console.log("挂掉了")
                // Tween.clear(this.tween)
                Laya.timer.clear(this, this.meteorCollision)
                if (this.isResurrection) {
                    console.log("22222", Mgr.circleList)
                    // Laya.timer.once(1000, this, this.resurrection);
                    this.resurrection()
                } else {
                    // Laya.timer.once(1000, this, this.gameOver);
                    this.isDouble()
                }
            }
        }
    }

    /*
    * 随机流星
    */
    randomMeteor(num: number = 0.35) {
        let randomNum = Math.random();
        if (randomNum > num) {
            let y0 = Mgr.circleList[0].y
            var isCollision: boolean = false
            this.meteor = new Image()
            this.meteor.skin = "images/meteor.png"
            
            this.meteor.size(120, 120).pos(this.width, y0 - 0).pivot(60,60)
            this.meteorList.push(this.meteor);
            Laya.stage.addChild(this.meteor)
            this.meteorTween = Tween.to(this.meteor, { x: 0, y: y0 + 800 }, 3000,Laya.Ease.quadIn,Laya.Handler.create(this,()=>{
                Laya.stage.removeChild(this.meteor)
            }))
            Laya.stage.frameLoop(1, this, this.meteorCollision)
            // this.meteorTween.update = new Laya.Handler(this, (res) => {

            // })
            // Laya.timer.once(3000, this, () => {
            //     Laya.stage.removeChild(this.meteor)
            // })

            if(Common.getCacheItem("meteorTips") == null){
                this.showAndHideTips("注意流星，一旦碰到流星则会失败");
                Common.setCacheItem("meteorTips","1");
            }
        }
    }

    /*
    * 监听飞机和流星的碰撞
    */
    meteorCollision() {
        let x = this.ball.x
        let x0 = Mgr.circleList[0].x
        if (this.ball.getBounds().intersection(this.meteor.getBounds())) {
            console.log("飞机和流星碰撞了挂掉了")
            Laya.stage.event("playSound", ["sound/boom.mp3"])
            this.boom = new Image();
            this.boom.skin = "images/boom.png"
            this.boom.size(140, 90)
            this.boom.pivot(70, 45).pos(this.meteor.x, this.meteor.y)
            Laya.stage.addChild(this.boom)
            if (x < x0) {
                this.boom.rotation = 90
                this.boom.rotation += this.degrees
            } else if (x > x0) {
                this.boom.rotation = 270
                this.boom.rotation += this.degrees
            }
            this.boom.zOrder = 5
            Laya.timer.clear(this, this.meteorCollision)
            Tween.clear(this.tween)
            if (this.isResurrection) {
                this.resurrection()
            } else {
                this.isDouble()
            }
        }
    }

    /**
     * 获取度数
     */
    public getDegrees(): number {
        let gap = this.getInsertPointBetweenCircleAndLine()
        let x
        let y
        let x0 = Mgr.circleList[0].x
        let y0 = Mgr.circleList[0].y
        gap.forEach(item => {
            if (item.y > Mgr.circleList[0].y) {
                x = item.x
                y = item.y
            }
        })
        let tan = (y - y0) / (x - x0)
        let result: number = Math.atan(tan) / (Math.PI / 180);
        result = Math.round(result);
        return result;
    }

    /**
     * 求圆和直线之间的交点
     * 直线方程：y = kx + b
     * 圆的方程：(x - m)² + (x - n)² = r²
     * x1, y1 = 线坐标1, x2, y2 = 线坐标2, m, n = 圆坐标, r = 半径
     */
    public getInsertPointBetweenCircleAndLine(r: number = Mgr.circleList[0].width / 2, index: number = 0): any[] {
        // console.log(x1, y1, x2, y2, m, n, r)
        let x1 = this.ball.x
        let y1 = this.ball.y
        let x2 = Mgr.circleList[index].x
        let y2 = Mgr.circleList[index].y
        let kbArr = this.binaryEquationGetKB(x1, y1, x2, y2)
        let k = kbArr[0]
        let b = kbArr[1]

        let aX = 1 + k * k
        let bX = 2 * k * (b - y2) - 2 * x2
        let cX = x2 * x2 + (b - y2) * (b - y2) - r * r

        let insertPoints = []
        let xArr = this.quadEquationGetX(aX, bX, cX)
        xArr.forEach(x => {
            let y = k * x + b
            insertPoints.push({ x: x, y: y })
        })
        return insertPoints
    }


    /**
     * 求二元一次方程的系数
     * y1 = k * x1 + b => k = (y1 - b) / x1
     * y2 = k * x2 + b => y2 = ((y1 - b) / x1) * x2 + b
     */
    private binaryEquationGetKB(x1, y1, x2, y2) {
        let k = (y1 - y2) / (x1 - x2)
        let b = (x1 * y2 - x2 * y1) / (x1 - x2)
        return [k, b]
    }


    /**
     * 一元二次方程求根
     * ax² + bx + c = 0
     */
    public quadEquationGetX(a, b, c) {
        let xArr = []
        let result = Math.pow(b, 2) - 4 * a * c
        if (result > 0) {
            xArr.push((-b + Math.sqrt(result)) / (2 * a))
            xArr.push((-b - Math.sqrt(result)) / (2 * a))
        } else if (result == 0) {
            xArr.push(-b / (2 * a))
        }
        return xArr
    }

    /**
     * 下降新增
     */
    newlyAdded(): void {
        let c = new Circle()
        let randomX: number = this.getRandom()
        let g = new Grade()
        let length = Mgr.circleList.length - 1
        let y = Mgr.circleList[length].y - 700
        if (this.lv <= 2) {
            this.gapAngle = 70
            Mgr.circleList.push(c.circle(randomX, y, 200))
            this.randomMeteor(0.6)
        } else if (this.lv <= 8) {
            this.gapAngle = 65
            Mgr.circleList.push(g.secondGrade(randomX, y, 200))
            this.randomMeteor(0.55)
        } else if (this.lv <= 14) {
            this.gapAngle = 60
            Mgr.circleList.push(g.thirdGrade(randomX, y, 200))
            this.randomMeteor(0.5)
        } else if (this.lv <= 20) {
            this.gapAngle = 55
            Mgr.circleList.push(g.fifthGrade(randomX, y, 200))
            this.randomMeteor(0.45)
        } else if (this.lv <= 26) {
            this.gapAngle = 50
            Mgr.circleList.push(g.fifthGrade(randomX, y, 200))
            this.randomMeteor(0.4)
        } else if (this.lv <= 32) {
            this.gapAngle = 45
            Mgr.circleList.push(g.sixthGrade(randomX, y, 200))
            this.randomMeteor(0.35)
        } else if (this.lv <= 38) {
            this.gapAngle = 40
            Mgr.circleList.push(g.seventhGrade(randomX, y, 200))
            this.randomMeteor(0.3)
        } else if (this.lv <= 44) {
            this.gapAngle = 35
            Mgr.circleList.push(g.eighthGrade(randomX, y, 200))
            this.randomMeteor(0.2)
        }
        this.lv++
        this.circularList.push(c.drawMidCircle(randomX, y, 100))
        this.randomProp()
        // Mgr.circleList[0].y = Mgr.circleList[0].y + 400
        // this.circularList[0].y = this.circularList[0].y + 400
        // if(this.propList.length>0){
        //     this.propList[0].y = this.propList[0].y + 400
        // }
    }

    /**
     * 下降
     */
    decline(): void {
        console.log("执行啦")
        // if (this.isAdd) {
        Mgr.circleList.forEach(item => {
            item.y = item.y + 2
        })
        this.circularList.forEach(item => {
            item.y = item.y + 2
        })
        this.lineList.forEach(item => {
            item.y = item.y + 2
        })
        this.spot.forEach(item => {
            item.y = item.y + 2
        })
        this.propList.forEach(item => {
            item.y = item.y + 2
        })
        // if (this.ball.y < 1073) {
        this.ball.y = this.ball.y + 2
        if (this.ball.y >= this.height) {
            Laya.timer.clear(this, this.decline)
            if (this.isResurrection) {
                Laya.timer.once(100, this, this.resurrection);
            } else {
                Laya.timer.once(100, this, this.isDouble);
            }
        }
        // }
        // }
    }

    /**
     * 140-500随机数
     */
    getRandom(): number {
        var n = Math.floor(Math.random() * this.Arr.length + 1) - 1;
        return this.Arr[n]
    }

    RandomNum() {
        var max = 300;
        var min = 1680;
        var random = Math.random() * (max - min) + min;
        return random
    }

    /**
     * 获取环旋转角度
     */
    getAngle(): void {
        if (Mgr.circleList.length > 0) {
            if (Mgr.circleList[0].rotation % 360 == 0) {
                this.angle = 0
            } else if (Mgr.circleList[0].rotation < 360) {
                this.angle = Mgr.circleList[0].rotation
            } else {
                this.angle = Mgr.circleList[0].rotation % 360
            }
        }
    }

    /**
     * 领取双倍奖励弹窗
     */
    isDouble() {
        let fractionNum = Number(this.fraction.text)
        let fractionGemstone
        if (fractionNum >= 1000) {
            fractionGemstone = Math.round(fractionNum / 1000) * 10
        } else {
            fractionGemstone = 0
        }
        let gemstoneNum = fractionGemstone += Number(this.gemstone.text)
        if (gemstoneNum != 0) {
            Laya.Scene.open("Dialog/Receive.scene", false, { type: "game", index: gemstoneNum })
        } else {
            this.gameOver(gemstoneNum)
        }
    }


    /**
    * 游戏结束
    */
    public gameOver(gemstoneNum: number): void {
        console.log("我执行了结束");
        Laya.stage.event("clearChange")
        Laya.stage.event("clearRingRotate")
        Laya.stage.event("clearStarRotate")
        Laya.stage.event("clear")
        Common.timeArray.forEach(item =>{
            Laya.timer.clearAll(item)
        })
        Common.timeArray = []
        Laya.timer.clearAll(Laya.stage);
        this.meteorList.forEach(item=>{
            Laya.stage.removeChild(item)
        })
        let fractionNum = Number(this.fraction.text)
        Laya.stage.removeChild(this.ball)
        Laya.stage.removeChild(this.boom)
        Mgr.circleList.forEach(item => {
            Laya.stage.removeChild(item)
        })
        Mgr.circleList.length = 0
        this.circularList.forEach(item => {
            Laya.stage.removeChild(item)
        })
        this.circularList.length = 0
        this.lineList.forEach(item => {
            Laya.stage.removeChild(item)
        })
        this.lineList.length = 0
        this.spot.forEach(item => {
            Laya.stage.removeChild(item)
        })
        this.spot.length = 0
        this.propList.forEach(item => {
            Laya.stage.removeChild(item)
        })
        this.propList.length = 0
        Laya.stage.event("RecordStop")
        Laya.Scene.open("gameOver.scene", true, { fractionNum: fractionNum, gemstoneNum: gemstoneNum, level: this.level })
    }


    /**
    * 复活
    */
    resurrection(): void {
        console.log("打开复活界面");
        console.log(Mgr.circleList);
        Laya.Scene.open('Dialog/resurrection.scene', false, 1)
        Laya.timer.clear(this, this.decline)
        this.isResurrection = false
    }

    /**
     * 复活移动
     */
    resurrectionTween(): void {
        console.log(Mgr.circleList)
        let x0 = Mgr.circleList[0].x
        let y0 = Mgr.circleList[0].y
        Tween.to(this.ball, { x: x0, y: y0 }, 1000);
        Laya.stage.removeChild(this.boom)
        Laya.stage.removeChild(this.propList[0])
        this.propList.splice(0, 1)
        Laya.timer.once(1000, this, this.remove);
    }

    // 发射小球管理方法
    public ballMgr(): void {
        console.log(22222)
        this.ball = this.b.ball();  //调用生成小球
        // this.box.addChild(this.ball)
        this.ball.on(Laya.Event.CLICK, this, this.onMouseDown);//监听鼠标点击
        this.bg.on(Laya.Event.CLICK, this, this.onMouseDown)

    }


}